<template>
  <div class="common-container">
    <head-title :title="title" />

    <div class="content-wrap">
      <div class="table-wrap ym-box">
        <div :style="{ height: tableHeight + 48 + 'px' }">
          <filter-wrap
            v-model:status-code="searchForm.statusVal"
            :status-data="issuesStatus"
            :form="searchForm"
            :all-def="false"
            :default-props="defaultProps"
            :center-options="state.centerOptions"
            @change-status="search"
            @search="commonSearch"
            @refresh="getStatusData"
          />
          <!-- :class="[issuesDetail ? 'has-detail' : '']" -->
          <div
            class="content"
            :style="{ height: 'calc(100% - 44px)' }"
            :class="[detail ? 'has-detail' : '']"
          >
            <div class="content-left">
              <ym-table
                v-loading="tableLoading"
                :data="tableData"
                :column-data="columnData"
                style="height: calc(100% - 48px)"
                height="calc(100% - 48px)"
                :operator-show="false"
                :auto-fixed="false"
                @row-click="handleIssuesClick"
              >
                <template #subject="{ row }">
                  <div class="issues-subject">
                    <svg-icon
                      class="info-avatar"
                      :icon-class="row.gender === '男' ? 'man' : 'woman'"
                      style="color: #646464"
                    />
                    <span class="issues-info__content">{{ row.sic }}</span>
                  </div>
                </template>
                <template #status="{ row }">
                  <span
                    class="issues-status"
                    :style="{ color: ISSUES_STATUS[row.issuesStatus]?.color }"
                    >{{ row.issuesStatusValue }}</span
                  >
                </template>
                <template #priority="{ row }">
                  <span v-if="row.priority">
                    <span
                      class="priority-circle"
                      :style="{ background: PRIORITY_CODES[row.priority.code]?.color }"
                    ></span>
                    <span class="issues-info__content">{{ row.priority.value }}</span>
                  </span>
                </template>
                <template #modality="{ row }">
                  <span v-if="row.modality" class="tag modality-tag">{{
                    row.modality.modality
                  }}</span>
                </template>
                <template #operator="{ row }">
                  <div
                    v-if="
                      row.issuesStatus !== ISSUES_STATUS_MAP.daihecha &&
                      row.issuesStatus !== ISSUES_STATUS_MAP.zhengchang
                    "
                  >
                    <el-button
                      class="confirm-text-button"
                      type="text"
                      @click="handleIssuesClick(row)"
                      >处理质疑</el-button
                    >
                  </div>
                  <el-button type="text" @click="handleOpenViewer(row)">查看影像</el-button>
                </template>
              </ym-table>
              <pagination
                v-model:currentPage="pageObj.pageNum"
                :page-size="pageObj.pageSize"
                :total="pageObj.total"
                @pagination="search"
              />
            </div>
            <div v-if="detail" class="content-right">
              <center-detail :detail="detail" />
              <common-wrap title="质疑" />
            </div>

            <!-- v-if="issuesDetail" -->
            <!-- <div v-if="currentIssues" class="content-right">
              <div class="close" @click="issuesDetail = null">
                <el-icon :size="20">
                  <close></close>
                </el-icon>
              </div>
              <div class="detail-item">
                <div class="detail-item__title">影像质量评估</div>
                <issues-detail :info="currentIssues" :detail="issuesDetail" :is-qc="isQc" />
              </div>
              <div
                v-if="currentIssues?.issuesStatus !== ISSUES_STATUS_MAP.daihecha && currentIssues?.issuesStatus !== ISSUES_STATUS_MAP.zhengchang"
                class="detail-item"
              >
                <div class="detail-item__title">回复</div>
                <reply
                  :info="currentIssues"
                  :detail="issuesDetail"
                  @search="submitReply"
                  @search-reply="searchReply"
                />
              </div>
            </div>-->
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script lang="ts" setup>
import { defineProps, reactive, toRefs } from 'vue'
import { useRoute } from 'vue-router'

import FilterWrap from './components/FilterWrap.vue'
import CommonWrap from '@/components/CommonWrap/index.vue'
import HeadTitle from '@/components/HeaderTitle/index.vue'
import { Pagination, usePage } from '@/components/Pagination/index'
import { YmTable } from '@/components/YmTable'
import CenterDetail from './CenterDetail/index.vue'

import { getOrgClsListByPid, orgType } from '@/api/common'
import VisitApi from '@/api/visit'

import { openViewer } from '@/utils/common/openViewer'
import { useTable } from '@/utils/common/useTable'

import CommonState from './commonState'
import {
  DCM_MODULE_FLAG_MAP,
  ISSUES_STATUS,
  ISSUES_STATUS_MAP,
  PAGE_FLAG,
  PRIORITY_CODES,
} from '@/configs/codeMaps'
import IBaseObj from '#/index'

const route = useRoute()

const { pid }: IBaseObj = route.query
const columnData = [
  {
    label: '状态',
    prop: 'issuesStatus',
    slotname: 'status',
  },
  {
    label: '访视',
    prop: 'name',
  },
  {
    label: '受试者',
    prop: 'sic',
    slotname: 'subject',
  },
  {
    label: '研究中心',
    prop: 'centre',
  },
]

const defaultProps = {
  key: 'value',
  value: 'value',
  num: 'count',
}

defineProps({
  title: {
    type: String,
    default: '质疑处理',
  },
})

const pageObj = reactive({ ...usePage() })
const { tableHeight } = useTable()
const state: IBaseObj = reactive({
  tableData: [],
  tableLoading: false,
  issuesStatus: [],
  centerOptions: [],
  timer: null,
  detail: null,
})
const { tableData, tableLoading, issuesStatus, detail } = toRefs(state)

/* search start */

const searchForm = reactive({
  pid: '',
  sic: '',
  centreId: '',
  statusVal: '',
})

// 获取状态
const getStatusData = async (init = true) => {
  const ret = await VisitApi.getIssuesStatus({
    pid: pid,
    flag: PAGE_FLAG.qc,
    centreId: searchForm.centreId,
    sic: searchForm.sic,
  })

  state.issuesStatus = ret || []
  if (init) {
    searchForm.statusVal = state.issuesStatus.length > 0 && state.issuesStatus[0].value
  }

  search()
}

const search = async () => {
  tableLoading.value = true
  try {
    const { sic, centreId, statusVal } = searchForm
    const statusObj = state.issuesStatus.find((v: IBaseObj) => v.value === statusVal)
    const { statusEnd, theEnd, status: status } = statusObj
    const ret = await VisitApi.getVisitByPage({
      sic,
      centreId,
      status,
      statusEnd,
      theEnd,
      pid,
      pageSize: pageObj.pageSize,
      pageNum: pageObj.pageNum,
    })
    const { records, total }: any = ret
    state.tableData = records || []
    pageObj.total = total || 0

    // 默认选中第一条
    // if (state.tableData?.length > 0) {
    //   handleIssuesClick(state.tableData[0])
    // }
  } finally {
    tableLoading.value = false
  }
}

const commonSearch = () => {
  getStatusData(false)
}

const getCenterOptions = async () => {
  let Pid = pid
  if (!pid) {
    // 如果选中项目的话 设置为0 比如qc角色进入的
    Pid = 0
  }
  const ret = await getOrgClsListByPid(Pid, orgType.yjdw)
  state.centerOptions = ret.data
}

/* search end */

/* 操作列表 start */

// 单击列表
const handleIssuesClick = async (issue: IBaseObj) => {
  const ret = await VisitApi.getVisitDicom(issue.vid)
  const dicomList = ret.data || []
  if (dicomList.length > 0) {
    state.detail = {
      ...issue,
      dicom: dicomList[0],
    }
  } else {
    state.detail = {
      ...issue,
      dicom: null,
    }
  }
}

// 打开影像
const handleOpenViewer = (issue: IBaseObj) => {
  const moduleFlag = DCM_MODULE_FLAG_MAP.chakan
  openViewer(
    {
      vid: issue.vid,
    },
    moduleFlag
  )
}

// const submitReply = () => {
//   if (currentState.currentIssues.issuesStatus === ISSUES_STATUS_MAP.yihuifu) {
//     searchReply()
//   } else {
//     getStatusData()
//   }
// }

// const searchReply = async () => {
//   const ret = await IssuesApi.getIssuesDetail(currentState.currentIssues.vid)
//   currentState.issuesDetail = ret
// }

// /* 操作列表 end */

const { getDictData } = CommonState
const init = () => {
  getCenterOptions()
  getStatusData()
  getDictData()
}
init()
</script>

<style lang="scss" scoped>
.common-container {
  padding: 0;
}

.content-wrap {
  padding: 0;

  .table-wrap {
    background: transparent;
  }
}

.issues {
  &-desc,
  &-time {
    color: var(--color-2);
  }

  &-status {
    display: inline-block;
    padding: 2px 8px;
    margin-right: 15px;
    border: 1px solid currentColor;
    border-radius: 4px;
  }

  &-desc {
    display: flex;
    margin-top: 10px;
    align-items: center;
  }

  &-subject {
    display: inline-flex;
    align-items: center;

    .info-avatar {
      margin-right: 10px;
    }
  }

  &-item {
    position: relative;
    padding: 10px 6px;
    cursor: pointer;
    border: 1px solid transparent;
    border-bottom: 1px solid var(--table-border);

    &.is-active {
      background: var(--table-background-striped);
      border-color: var(--color-primary);
      border-radius: 6px;

      &::before {
        position: absolute;
        top: -1px;
        bottom: -1px;
        left: -1px;
        border-left: 5px solid var(--color-primary);
        border-radius: 6px 0 0 6px;
        content: '';
      }
    }

    &:hover {
      background: #fafafb;
    }
  }

  &-title {
    margin-right: 30px;

    &__content {
      font-size: 18px;
      font-weight: 600;
    }
  }

  &-info {
    &__label {
      flex-shrink: 0;
    }

    &__content {
      font-size: 16px;
    }
  }

  &-list {
    height: calc(100% - 48px);
    overflow: auto;
    @include scroll-bar;
  }
}

.table {
  &-body {
    height: 100%;
  }
}

.pr10 {
  padding-right: 10px;
}

.df-ac {
  display: flex;
  align-items: center;
}

.priority-circle {
  display: inline-block;
  width: 10px;
  height: 10px;
  margin-right: 4px;
  vertical-align: middle;
  border-radius: 50%;
}

.content {
  display: flex;

  &.has-detail {
    .content-left {
      width: 40%;
      // width: 60%;
      // min-width: calc(100% - 400px);
      padding-right: 10px;
    }

    .content-right {
      // width: 40%;
      width: 60%;
      // min-width: 400px;
      // display: flex;

      .center-detail {
        // flex: 1;

        // border-right: 1px solid var(--table-border);
      }

      .reply-wrap {
        width: 30%;
        padding-left: 20px;
        flex-shrink: 0;
      }
    }
  }

  & > div {
    height: 100%;
  }

  &-left {
    // flex: 1;
    width: 100%;
    padding-top: 4px;
    transition: width 0.3s;
  }

  &-right {
    position: relative;
    width: 0;
    padding: 10px;
    // padding: 20px;
    overflow: auto;
    border-left: 1px solid var(--table-border);
    transition: width 0.3s;
    @include scroll-bar;

    // & > div {
    //   padding: 10px;
    //   height: 100%;
    //   overflow: auto;
    //   @include scroll-bar;
    // }

    .close {
      position: absolute;
      top: 20px;
      right: 20px;
      cursor: pointer;

      &:hover {
        color: var(--color-primary);
      }
    }
  }
}

.detail-item {
  padding-bottom: 18px;
  margin-bottom: 20px;
  border-bottom: 1px solid var(--table-border);

  &:last-child {
    border-bottom: 0;
  }

  &__title {
    margin-bottom: 10px;
    // font-size: 16px;
    font-weight: 600;
  }
}

.timeout-icon {
  margin-right: 10px;
  font-size: 20px;
  color: red;
  vertical-align: top;
}
</style>
